package com.example.Administrator.shoujiweishi.activities.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.Administrator.shoujiweishi.MyApplication;
import com.example.Administrator.shoujiweishi.R;
import com.example.Administrator.shoujiweishi.activities.Insert_contacts;
import com.example.Administrator.shoujiweishi.utils.CircleTextImageView;
import com.example.Administrator.shoujiweishi.vo.Contact;
import com.github.lzyzsd.randomcolor.RandomColor;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/6/6 0006.
 */

public class ShowAllAdapter extends RecyclerView.Adapter<ShowAllAdapter.ViewHolder>{

    String edtext="";
    static HashMap<String,Contact> hashMap=new HashMap();
    private List<Contact> list;
    private View itemView;

    public ShowAllAdapter(List<Contact> list){
        this.list=list;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name,phonenum,address;
        private CircleTextImageView namep;
        private CheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            name= (TextView) itemView.findViewById(R.id.name);
            address= (TextView) itemView.findViewById(R.id.address);
            phonenum= (TextView) itemView.findViewById(R.id.phonenum);
            namep= (CircleTextImageView) itemView.findViewById(R.id.namep);
            checkBox= (CheckBox) itemView.findViewById(R.id.checkboxuser);

        }
    }
    @Override
    public ShowAllAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = LayoutInflater.from(MyApplication.getContext())
                .inflate(R.layout.showcontact_item,parent,false);

        ShowAllAdapter.ViewHolder viewHolder = new ShowAllAdapter.ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final Contact contact=list.get(position);

        holder.name.setText(contact.getName());
        holder.address.setText(contact.getAddress());
        holder.phonenum.setText(contact.getPhonenum().replace(" ","").replace("-",""));
        holder.namep.setText(contact.getName().substring(1));
        RandomColor randomColor=new RandomColor();
        int color=randomColor.randomColor();
        holder.namep.setFillColor(color);
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    if (contact.getPhonenum().replace(" ","").replace("-","").length()==11||contact.getPhonenum().matches("^1[34578]\\d{9}$")){
                        if (hashMap.size()<3){
                            hashMap.put(contact.getPhonenum(),contact);
                            Insert_contacts.textView.setText("选择联系人（"+hashMap.size()+"）");
                            edtext=edtext+contact.getPhonenum()+",";
                            Insert_contacts.insertcontacts.setText(edtext);
                            Toast.makeText(MyApplication.getContext(), hashMap.get(contact.getPhonenum()).getName()+"\n"+hashMap.get(contact.getPhonenum()).getAddress()+"\n"+hashMap.get(contact.getPhonenum()).getPhonenum(), Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(MyApplication.getContext(), "最多选三个", Toast.LENGTH_SHORT).show();
                            holder.checkBox.setChecked(false);
                        }
                    }else{
                        holder.checkBox.setChecked(false);
                    }

                }else{
                    hashMap.remove(contact.getPhonenum());
                    Insert_contacts.textView.setText("选择联系人（"+hashMap.size()+"）");
                    edtext=edtext.replace(contact.getPhonenum()+",","");
                    Insert_contacts.insertcontacts.setText(edtext);
                }
            }
        });
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyApplication.getContext(), "点击事件", Toast.LENGTH_SHORT).show();
                holder.checkBox.setChecked(true);
            }
        });
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
}
